<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    width="63%"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="主题名称" prop="name"
                :rules="[
                  {required: true, message:'主题名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写主题名称"></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="英文名称" prop="enname"
                :rules="[
                  {required: true, message:'英文名称不能为空', trigger:'blur'},
                  {validator: validator.isEnglish, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.enname" placeholder="请填写英文名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="样式" prop="style"
                :rules="[
                 ]">
              <el-input v-model="inputForm.style" placeholder="请填写样式"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="激活状态" prop="activa"
                :rules="[
                 ]">
              <el-switch
                v-model="inputForm.activa"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="1"
                inactive-value="0">
              </el-switch>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>

            </el-form-item>
        </el-col>
        <el-col v-if="method!=='view'">
          <el-tag type="info">参数配置</el-tag>
          <vue-json-editor
            v-if="method!=='view'"
            ref="paramConfigEditor"
            :show-btns="false"
            v-model="inputForm.jsonParam"
            :mode="'code'"
            lang="zh"
            @json-change="onJsonChange">
          </vue-json-editor>
        </el-col>
        <el-col :span="12" v-else>
          <el-form-item label="参数配置" prop="paramConfig"
                        :rules="[
                 ]">
            <el-input type="textarea" v-model="inputForm.paramConfig" placeholder="请填写参数配置"></el-input>

          </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
import vueJsonEditor from 'vue-json-editor'

export default {
  data () {
    return {
      title: '',
      method: '',
      visible: false,
      loading: false,
      inputForm: {
        id: '',
        name: '',
        enname: '',
        style: '',
        activa: 1,
        paramConfig: '',
        remarks: '',
        jsonParam: {}
      }
    }
  },
  components: {
    vueJsonEditor
  },
  methods: {
    init (method, id) {
      this.method = method
      this.inputForm.id = id
      if (method === 'add') {
        this.title = `新建主题`
      } else if (method === 'edit') {
        this.title = '修改主题'
      } else if (method === 'view') {
        this.title = '查看主题'
      }
      this.visible = true
      this.loading = false
      this.$nextTick(() => {
        this.$refs.inputForm.resetFields()
        if (method === 'edit' || method === 'view') { // 修改或者查看
          this.loading = true
          this.$http({
            url: `/cms/theme/cmsThemes/queryById?id=${this.inputForm.id}`,
            method: 'get'
          }).then(({data}) => {
            this.inputForm = this.recover(this.inputForm, data.cmsThemes)
            try {
              this.inputForm.jsonParam = JSON.parse(this.inputForm.paramConfig)
            } catch (e) {
              this.inputForm.jsonParam = this.inputForm.paramConfig
            }
            this.loading = false
          })
        }
      })
    },
      // 表单提交
    doSubmit () {
      this.$refs['inputForm'].validate((valid) => {
        if (valid) {
          this.loading = true
          this.$http({
            url: `/cms/theme/cmsThemes/save`,
            method: 'post',
            data: this.inputForm
          }).then(({data}) => {
            this.loading = false
            if (data && data.success) {
              this.visible = false
              this.$message.success(data.msg)
              this.$emit('refreshDataList')
            }
          })
        }
      })
    },
    onJsonChange (value) {
      this.inputForm.paramConfig = JSON.stringify(value)
    }

  }
}
</script>
